#include "Server.h"

int main(char* args) {
	cout << "Starting rs2++\n";

	Server::player_container = new PlayerContainer(MAX_PLAYERS);
	Server::load_external_files();

	thread thr = thread(Server::run);
	thr.join();
}

namespace Server
{
	void Server::load_external_files() {
		
	}

	void Server::run() {
		Server::running = true;
		Socket listener;
		if(listener.listen(PORT) == -1)
		{
			cout << "Error starting server.\n";
#ifdef _WIN32
			cout << "Error code " << WSAGetLastError() << "\n";
#endif
			cin.get();
			return;
		}
		cout << "Listening on port " << PORT << "\n";

		while(Server::running) {
			Socket* accept = listener.accept();
			Server::player_container->handle_connection(accept);

		}

		cout << "Listener thread terminated. Exiting program.\n";
		cin.get();
	}

}